/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.statusbar

import android.car.hardware.CarPropertyValue
import android.car.hardware.property.CarPropertyManager
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.controller.CenterLockController
import com.ecarx.systemui.plugin.ui.custom.SystemBarButton
import com.ecarx.systemui.plugin.utils.LogUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CenterLockView : SystemBarButton {
    private var centerLockController: CenterLockController = CenterLockController.getInstance()

    constructor(context: Context?) : this(context!!, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context!!, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    )

    override fun getContentDescriptionString(): String {
        return "CenterLockView"
    }

    private val callback = object : CarPropertyManager.CarPropertyEventCallback {
        override fun onChangeEvent(value: CarPropertyValue<*>?) {
            LogUtils.d(TAG, "onChangeEvent:$value")
            refreshView()
        }

        override fun onErrorEvent(p0: Int, p1: Int) {

        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        CoroutineScope(Dispatchers.IO).launch {
            centerLockController.registerCallback(callback)
        }

        setOnClickListener {
            CoroutineScope(Dispatchers.IO).launch {
                val isLock = centerLockController.getCenterLockState()
                LogUtils.d(TAG, "setCenterLock:${!isLock}")
                centerLockController.setCenterLock(!isLock)
            }
        }
        refreshView()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        CoroutineScope(Dispatchers.IO).launch {
            centerLockController.unRegisterCallback(callback)
        }
    }

    private fun refreshView() {
        CoroutineScope(Dispatchers.IO).launch {
            val isAvailable = centerLockController.isCenterLockAvailable()
            val isLocked = centerLockController.getCenterLockState()
            LogUtils.d(TAG,"refreshView")
            withContext(Dispatchers.Main) {
                if (isAvailable) {
                    visibility = View.VISIBLE
                    LogUtils.d(TAG,"centerLock isAvailable")
                    val icon = if (isLocked)
                        R.drawable.ic_status_center_lock_locked
                    else
                        R.drawable.ic_status_center_lock_unlock
                    setImageDrawable(ResourcesCompat.getDrawable(resources, icon, context.theme))
                } else {
                    LogUtils.d(TAG,"centerLock unAvailable")
                    visibility = View.GONE
                }
            }
        }
    }

    companion object {
        private const val TAG = "CenterLockView"
    }

}